//
//  TitleViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "TitleViewController.h"
#import "AfNetWorkingHandler.h"
#import "QuestionModel.h"
#import "AnswerModel.h"
#import "QuestionCell.h"
#import <UIImageView+WebCache.h>
#import "BlackManager.h"
#import "SetBlack.h"
#import "PushWebViewVC.h"
@interface TitleViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSDictionary *expert;//专家信息
@property (nonatomic, strong)NSMutableArray *hotArr;//热门提问数据
@property (nonatomic, strong)NSMutableArray *latesArr;//最新提问数据
@property (nonatomic, strong)NSMutableArray *questionArr;
@property (nonatomic, strong)NSMutableArray *answerArr;
@end

@implementation TitleViewController
- (void)dealloc
{
    [_model release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-newlisticon06"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)]autorelease];
    //tableView
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
//    _tableView.backgroundColor = [UIColor cyanColor];
    //相关新闻cell
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newsCell"];
    //问答cell
    [_tableView registerClass:[QuestionCell class] forCellReuseIdentifier:@"titleCell"];
    [self.view addSubview:_tableView];
    //tableView头部
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 330)];
    [SetBlack setBackgroundColorWithView:header];
    //背景图
    UIImageView *backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 220)];
    [backImg sd_setImageWithURL:[NSURL URLWithString:_model.picurl]];
    [header addSubview:backImg];
    [backImg release];
    //添加的模糊图片
    UIImageView *mohuImg = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 5, backImg.frame.size.height / 4, backImg.frame.size.width / 5 * 3, 70)];
    mohuImg.alpha = 0.05;
    mohuImg.image = [UIImage imageNamed:@"mohu.jpg"];
    [backImg addSubview:mohuImg];
    [mohuImg release];
    //作者介绍
    UILabel *jieshaoLable = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 5, backImg.frame.size.height / 4, backImg.frame.size.width / 5 * 3, 70)];
    jieshaoLable.numberOfLines = 2;
    jieshaoLable.font = [UIFont systemFontOfSize:16];
    jieshaoLable.text = _model.alias;
    jieshaoLable.textColor = [UIColor whiteColor];
    [backImg addSubview:jieshaoLable];
    [jieshaoLable release];
    //关注数量
    UILabel *concernLable = [[UILabel alloc]initWithFrame:CGRectMake(backImg.frame.size.width / 3 + 5, mohuImg.frame.origin.y + mohuImg.frame.size.height + 5, backImg.frame.size.width / 3, 30)];
    concernLable.text = [NSString stringWithFormat:@"- %@关注 -",_model.concernCount.stringValue];
    [backImg addSubview:concernLable];
    concernLable.textColor = [UIColor whiteColor];
    [concernLable release];
    //添加关注按钮
    UIButton *concerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    concerBtn.frame = CGRectMake(concernLable.frame.origin.x + 5, concernLable.frame.origin.y + concernLable.frame.size.height + 5, concernLable.frame.size.width - 30, 30);
    concerBtn.backgroundColor = [UIColor redColor];
    [concerBtn setTitle:@"＋ 关注" forState:UIControlStateNormal];
    [concerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    concerBtn.layer.cornerRadius = 10;
    concerBtn.clipsToBounds = YES;
    [backImg addSubview:concerBtn];
    //作者介绍
    UIImageView *expertImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, backImg.frame.origin.y + backImg.frame.size.height + 10, 40, 40)];
    [expertImg sd_setImageWithURL:[NSURL URLWithString:_model.headpicurl]];
    expertImg.layer.cornerRadius = 20;
    expertImg.clipsToBounds = YES;
    [header addSubview:expertImg];
    [expertImg release];
    //作者姓名
    UILabel *nameLable = [[UILabel alloc]initWithFrame:CGRectMake(expertImg.frame.origin.x + expertImg.frame.size.width + 5, expertImg.frame.origin.y, header.frame.size.width / 5 * 4, 25)];
    nameLable.text = [[_model.name stringByAppendingString: @"|"]stringByAppendingString:_model.title];
    nameLable.numberOfLines = 0;
    nameLable.font = [UIFont systemFontOfSize:13];
    nameLable.textColor = [UIColor grayColor];
    [header addSubview:nameLable];
    [nameLable release];
    
    UILabel *descripLable = [[UILabel alloc]initWithFrame:CGRectMake(nameLable.frame.origin.x, nameLable.frame.origin.y + nameLable.frame.size.height + 5, header.frame.size.width / 5 * 4, 60)] ;
    descripLable.numberOfLines = 0;
    descripLable.textColor = [UIColor grayColor];
    descripLable.text = _model.expertDescription;
    descripLable.font = [UIFont systemFontOfSize:14];
    [header addSubview:descripLable];
    _tableView.tableHeaderView = header;
    [descripLable release];
    [header release];
    [_tableView release];
    
    //数据处理
    [self dataHandler];
}
-(void)leftAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        //
    }];
}
//数据处理
-(void)dataHandler
{
    NSString *path = [NSString stringWithFormat:@"http://c.m.163.com/newstopic/qa/%@.html", _model.expertId];
    [AfNetWorkingHandler GetWithStr:path complection:^(id result) {
        //
        NSLog(@"%@", result);
        //专家信息
        self.expert = result[@"data"][@"expert"];
        //最热提问
//        self.hotArr = result[@"data"][@"hotList"];
        NSDictionary *dic = result[@"data"];
        self.hotArr = dic[@"hotList"];
        NSMutableArray *qArr = [NSMutableArray array];
        NSMutableArray *aArr = [NSMutableArray array];
        for (NSDictionary *dic in _hotArr) {
            NSDictionary *questionDic = dic[@"question"];
            NSDictionary *answerDic = dic[@"answer"];
            [qArr addObject:questionDic];
            [aArr addObject:answerDic];
        }
        self.questionArr = [QuestionModel modelHanderWithArray:qArr];
        self.answerArr = [AnswerModel modelHanderWithArray:aArr];
        //最新提问
        self.latesArr = result[@"data"][@"latestList"];
        
        [_tableView reloadData];
    }];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        //获取新闻的id
        NSArray *arr = _expert[@"relatedNews"];
        __block NSString *strId = [arr[indexPath.row] objectForKey:@"docid"];
        PushWebViewVC *relatedVC = [[PushWebViewVC alloc ]init];
        //获取网址
        NSString *strhtml = [NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/hot/3g_bbs/%@/0/10/10/2/2", strId];
        [AfNetWorkingHandler GetWithStr:strhtml complection:^(id result) {
            
            NSString *aa = result[@"docUrl"];
            relatedVC.pathstr = aa;
            [self.navigationController pushViewController:relatedVC animated:YES];
            [relatedVC release];
        }];
    }
}
//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
//cell个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [_expert[@"relatedNews"] count];
    }
    if (section == 1) {
        return _questionArr.count;
    }else{
        return 0;
    }
}
//自定义头部区域
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //创建放在分区头的view 在他的上面放其他东西
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    if ([BlackManager sharedInstance].isNight) {
        header.backgroundColor = [UIColor blackColor];
    }else{
         header.backgroundColor = [UIColor lightGrayColor];
    }
    //相关新闻
    if (section == 0) {
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 26, 20)];
        img.image = [UIImage imageNamed:@"iconfont-news-2"];
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(img.frame.origin.x + img.frame.size.width + 5, img.frame.origin.y, 4 * img.frame.size.width, 22)];
        lable.text = @"相关新闻";
        lable.textColor = [UIColor grayColor];
        lable.font = [UIFont systemFontOfSize:14];
        [header addSubview:lable];
        [header addSubview:img];
        [img release];
        [lable release];
    }
    //问答
    if (section == 1) {
        //提问回复lable
        UILabel *questionLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, self.view.frame.size.width / 3, 20)];
        NSNumber *questionNum = [_expert objectForKey:@"questionCount"];
        NSNumber *answerNum = [_expert objectForKey:@"answerCount"];
        questionLable.text = [NSString stringWithFormat:@"%ld提问 · %ld回复", questionNum.integerValue, answerNum.integerValue];
        questionLable.textColor = [UIColor grayColor];
        questionLable.font = [UIFont systemFontOfSize:14];
        [header addSubview:questionLable];
        [questionLable release];
    }
    
    return [header autorelease];
}
//头部区域高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
//设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 50;
    }else{
        QuestionModel *qmodel = _questionArr[indexPath.row];
        CGFloat questionH = [QuestionCell heightWithText:qmodel.content];
        AnswerModel *amodel = _answerArr[indexPath.row];
        CGFloat answerH = [QuestionCell heightWithText:amodel.content];
        return questionH + answerH + 140;
    }
}
//页面出现前更新页面
-(void)viewWillAppear:(BOOL)animated
{
    [_tableView reloadData];
}
-(void)setNightDayModel
{
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    [SetBlack setBackgroundColorWithView:_tableView];
}
-(void)setLightDayModel
{
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    [SetBlack setBackgroundColorWithView:_tableView];
}
//cell详情设计
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //相关新闻
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsCell"];
        [SetBlack setCellColorWithCell:cell];
        [SetBlack setLableTitleColorWithArr:@[cell.textLabel]];
        NSArray *arr = _expert[@"relatedNews"];
        cell.textLabel.text = arr[indexPath.row][@"title"];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        return cell;
    }
    if (indexPath.section == 1) {
        QuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"titleCell" forIndexPath:indexPath];
        if (_questionArr.count != 0 && _answerArr.count != 0) {
            QuestionModel *qmodel = _questionArr[indexPath.row];
            AnswerModel *amodel = _answerArr[indexPath.row];
            [SetBlack setCellColorWithCell:cell];
            [SetBlack setLableTitleColorWithArr:@[cell.answerLable]];
            //传值计算自定义高度
            cell.qcontent = qmodel.content;
            cell.acontent = amodel.content;
            [cell.askerImg sd_setImageWithURL:[NSURL URLWithString:qmodel.userHeadPicUrl]];
            cell.askerLabel.text = qmodel.questionId;
            cell.questionLabel.text = qmodel.content;
            [cell.expertImg sd_setImageWithURL:[NSURL URLWithString:amodel.specialistHeadPicUrl]];
            cell.expertLable.text = amodel.specialistName;
            cell.answerLable.text = amodel.content;
            }
        return cell;
    }else{
        return 0;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
